<template>
  <BasicModal
    :footer="null"
    :title="t('layout.header.lockScreen')"
    v-bind="$attrs"
    :class="prefixCls"
    @register="register"
  >
    <div :class="`${prefixCls}__entry`">
      <div :class="`${prefixCls}__header`">
        <img :src="avatar" :class="`${prefixCls}__header-img`" />
        <p :class="`${prefixCls}__header-name`">
          {{ getRealName }}
        </p>
      </div>

      <a-form :model="formData" :label-col="{ span: 4 }" :wrapper-col="{ span: 18 }">
        <a-form-item label="密码" v-bind="validateInfos.password">
          <a-input v-model:value="formData.password" />
        </a-form-item>
      </a-form>

      <div :class="`${prefixCls}__footer`">
        <a-button type="primary" block class="mt-2" @click="handleLock">
          {{ t('layout.header.lockScreenBtn') }}
        </a-button>
      </div>
    </div>
  </BasicModal>
</template>
<script lang="ts">
export default {
  name: 'LockModal',
};
</script>
<script setup lang="ts">
import { computed, ref } from 'vue';
import { useI18n } from '/@/hooks/web/useI18n';
import { useDesign } from '/@/hooks/web/useDesign';
import { Form, message } from 'ant-design-vue';
import { BasicModal, useModalInner } from '/@/components/Modal/index';

import { useUserStore } from '/@/store/modules/user';
import { useLockStore } from '/@/store/modules/lock';
import headerImg from '/@/assets/images/header.jpg';

const useForm = Form.useForm;
const { t } = useI18n();
const { prefixCls } = useDesign('header-lock-modal');
const userStore = useUserStore();
const lockStore = useLockStore();

const getRealName = computed(() => userStore.getUserInfo?.realName);
const [register, { closeModal }] = useModalInner();

const rules = ref({
  password: [{ required: true, trigger: 'change', message: '请输入密码' }],
});

const formData = ref({
  password: '',
});

const { resetFields, validate, validateInfos } = useForm(formData, rules);

async function handleLock() {
  await validate();
  closeModal();

  lockStore.setLockInfo({
    isLock: true,
    pwd: formData.value.password,
  });
  await resetFields();
}

const avatar = computed(() => {
  const avatar = userStore.getUserInfo?.avatar;
  return avatar || headerImg;
});
</script>
<style lang="less">
@prefix-cls: ~'@{namespace}-header-lock-modal';

.@{prefix-cls} {
  &__entry {
    position: relative;
    //height: 240px;
    padding: 130px 30px 30px;
    border-radius: 10px;
  }

  &__header {
    position: absolute;
    top: 0;
    left: calc(50% - 45px);
    width: auto;
    text-align: center;

    &-img {
      width: 70px;
      border-radius: 50%;
    }

    &-name {
      margin-top: 5px;
    }
  }

  &__footer {
    text-align: center;
  }
}
</style>
